package gui;

import dto.UserDTO;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import logic.User;

/**
 *
 * @author lucifer
 */
public class UserPasswordDialog extends javax.swing.JDialog {

  public static final int RET_CANCEL = 0;
  public static final int RET_OK = 1;

  
  //TODO: String
  private String passwd;

  public UserPasswordDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();

    String cancelName = "cancel";
    InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
    ActionMap actionMap = getRootPane().getActionMap();
    actionMap.put(cancelName, new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        doClose(RET_CANCEL);
      }
    });
  }

  public String GetPasswd() {
    return passwd;
  }

  public int getReturnStatus() {
    return returnStatus;
  }

  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    okButton = new javax.swing.JButton();
    
  //TODO:
    txtConNueva = new javax.swing.JPasswordField();
    newPasswdjLabel = new javax.swing.JLabel();
    confirmNewPasswdjLabel = new javax.swing.JLabel();
    //TODO:
    txtConNueva2 = new javax.swing.JPasswordField();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Nuevo Password");
    setResizable(false);
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        closeDialog(evt);
      }
    });

    okButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/aceptar.png"))); // NOI18N
    okButton.setText("Listo");
    okButton.setMaximumSize(new java.awt.Dimension(112, 27));
    okButton.setMinimumSize(new java.awt.Dimension(112, 27));
    okButton.setPreferredSize(new java.awt.Dimension(112, 27));
    okButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        okButtonActionPerformed(evt);
      }
    });

    txtConNueva.setMaximumSize(new java.awt.Dimension(292, 27));
    txtConNueva.setMinimumSize(new java.awt.Dimension(292, 27));
    txtConNueva.setPreferredSize(new java.awt.Dimension(292, 27));
    txtConNueva.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        newPasswdjPasswordFieldFocusGained(evt);
      }
    });

    newPasswdjLabel.setText("Nuevo password");

    confirmNewPasswdjLabel.setText("Confirmar nuevo password");

    txtConNueva2.setMaximumSize(new java.awt.Dimension(292, 27));
    txtConNueva2.setMinimumSize(new java.awt.Dimension(292, 27));
    txtConNueva2.setPreferredSize(new java.awt.Dimension(292, 27));
    txtConNueva2.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        confirmNewPasswdjPasswordFieldFocusGained(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGap(12, 12, 12)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(newPasswdjLabel, javax.swing.GroupLayout.Alignment.TRAILING)
              .addComponent(confirmNewPasswdjLabel, javax.swing.GroupLayout.Alignment.TRAILING))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(txtConNueva2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addComponent(txtConNueva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
        .addGap(12, 12, 12))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addGap(12, 12, 12)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(txtConNueva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(newPasswdjLabel))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(txtConNueva2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(confirmNewPasswdjLabel))
        .addGap(12, 12, 12)
        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(12, 12, 12))
    );

    getRootPane().setDefaultButton(okButton);

    pack();
  }// </editor-fold>//GEN-END:initComponents

  
//  TODO: la clave para entender toda la magia
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
      if (txtConNueva.getPassword().length == 0 || txtConNueva2.getPassword().length == 0) {
        JOptionPane.showMessageDialog(this, "Nuevo password y/ó confirmación vacía. Por favor, intente nuevamente", "Atención!", JOptionPane.WARNING_MESSAGE);
        txtConNueva.setText("");
        txtConNueva2.setText("");
        return;
      }

      if (!String.valueOf(txtConNueva.getPassword()).equals(String.valueOf(txtConNueva2.getPassword()))) {
        JOptionPane.showMessageDialog(this, "El nuevo password y su confirmación deben coincidir. Por favor, intente nuevamente", "Atención!", JOptionPane.WARNING_MESSAGE);
        txtConNueva.setText("");
        txtConNueva2.setText("");
        return;
      }

      passwd = String.valueOf(txtConNueva.getPassword());
      doClose(RET_OK);
    }//GEN-LAST:event_okButtonActionPerformed

    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
      doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog

  private void newPasswdjPasswordFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_newPasswdjPasswordFieldFocusGained
    txtConNueva.selectAll();
  }//GEN-LAST:event_newPasswdjPasswordFieldFocusGained

  private void confirmNewPasswdjPasswordFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_confirmNewPasswdjPasswordFieldFocusGained
    txtConNueva2.selectAll();
  }//GEN-LAST:event_confirmNewPasswdjPasswordFieldFocusGained
  
  private void doClose(int retStatus) {
    returnStatus = retStatus;
    setVisible(false);
    dispose();
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel confirmNewPasswdjLabel;
  private javax.swing.JPasswordField txtConNueva2;
  private javax.swing.JLabel newPasswdjLabel;
  private javax.swing.JPasswordField txtConNueva;
  private javax.swing.JButton okButton;
  // End of variables declaration//GEN-END:variables

  private int returnStatus = RET_CANCEL;
}
